package org.example.linear;

public class SeqList<T> {
    private final int INITAL_CAP=10;

    private int site;
    private T[] list;

    public SeqList() {
        this.list = (T[]) new Object[INITAL_CAP];
        this.site = 0;
    }

    public SeqList(int i) {
        this.list = (T[]) new Object[i];
        this.site = 0;
    }

    public void clear() {
        this.site = 0;
    }

    public boolean isEmpty() {
        return site == 0;
    }

    public int siteNow() {
        return site;
    }

    public T getMyValue(int i) {
        if (i < 0 || i > site) {
            throw new RuntimeException("超出范围");
        }
        return list[i];
    }

    public int getMySite(T t){
        for (int i =0;i<site;i++){
            if (list[i].equals(t)){
                return i;
            }
        }
        return -1;
    }

    public void insert(T t){
        if (site==0){
            SeqList seqList = new SeqList();
        }else if(list[site-1]!=null){
            throw new RuntimeException("超出长度");
        }
        list[site++]=t;
    }

    public void insert(T t,int s){
        if (s<0||s>site){
            throw new RuntimeException("超出范围");
        }
        for (int i=site;site>s;i--){
            list[i]=list[i-1];
        }
        site++;
    }

    public void delete(T t,int s){
        if (s<0||s>site){
            throw new RuntimeException("超出范围");
        }
        for (int i=s;s<site;i++){
            list[i]=list[i+1];
        }
        site--;
    }
}
